var empid = 0;
var funzzid = 0;
var fun = [];
var funid = [];
$(function(){
	//初始化表格
	$('#authoritytable').datagrid({
	    url:'authority_t',
	    columns:[[
	    	{field:'empname',width:80,title:'姓名',align:'center'},
			{field:'position',width:180,title:'职位',align:'center'},
			{field:'caozuo',width:110,title:'操作',align:'center',formatter:chaxun}
	    ]],
	    pagination:true,
	    pageNumber:1,
	    pageSize:9,
	    pageList:[5,9]
	});
	$(document).on("click",".atl",function(){
		var aut = $(this);
		if(aut.attr("class")=="atl at_h"){
			aut.removeClass("at_h");
			aut.addClass("at_g");
		}else{
			aut.removeClass("at_g");
			aut.addClass("at_h");
		}
	});
	$(document).on("click",".autho_a_t7",function(){
		funid[funzzid] = $(this).attr("id");
		var aut = $(this).find("div");
		if(aut.attr("class")=="len"){
			fun[funzzid] = "false";
			aut.removeClass("len");
			aut.addClass("rin");
		}else{
			fun[funzzid] = "true";
			aut.removeClass("rin");
			aut.addClass("len");
		}
		funzzid++;
	});
	
	
	$(document).on("click",".autho_ta_1",function(){
		var text = $(this).text();
		$.ajax({
			url: "authority_r",
			data:{
				empid:empid,
				text:text
			},
			success: function(data){
				$.ajax({
					url: "tail-3",
					data:{
						empid:empid,
						tatext:text
					},
					success: function(daa){
						$(".autho_a_t7").remove();
						for(var i=0;data.length>i;i++){
							var html = "<div class='autho_a_t7 len' id='"+data[i]['funid']+"'><div class='rin'></div>"+data[i]['funname']+"</div>";
							$(".autho_a_t").append(html);
						}
						var autlen = $(".autho_a_t7").length;
						for(var i=0;autlen>i;i++){
							var the = $(".autho_a_t7:eq("+i+")");
							if(parseInt(the.attr("id"))==parseInt(daa[i]['funid'])){
								the.find(".rin").addClass("len");
								the.find(".rin").removeClass("rin");
							}
						}
					}
				});
			}
		});
	});
});
function chaxun(value, row, index) {
	var pape = $(".pagination-num").val();
	var row = $(".pagination-page-list").val();
	return "<a onclick='aut_ta("+((row*(pape-1))+index+1)+")'  class='easyui-linkbutton'>修改</a>";
}
function aut_ta(i){
	$(".autho_a_b .green").attr('onclick',"preser("+i+")");
	empid = i;
	$.ajax({
		url: "authority_e",
		data:{
			empid:i
		},
		success: function(data){
			$.ajax({
				url: "tail-1",
				data:{
					empid:i
				},
				success: function(daa){
					$(".autho_ta_1").remove();
					for(var i=0;data.length>i;i++){
						var html = "<div class='autho_ta_1' id='"+data[i]['pesid']+"' title='"+data[i]['pesname']+"'><p class='atl at_h'></p>"+data[i]['pesname']+"</div>";
						$(".autho_a_l").append(html);
					}
					for(var i=0;daa.length>i;i++){
						var the = $(".atl:eq("+(daa[i]['pesid']-1)+")");
						the.addClass("at_g");
						the.removeClass("at_h");
					}
					$(".green,.red").removeClass("none");
				}
			});
			
		}
	});
}
function preser(empid){
	var x=$(".autho_ta_1").length;
	for(var y=0;y<x;y++){
		var st="";
		if($(".autho_ta_1:eq("+y+")").find("p").attr("class")=="atl at_g"){
			st="true";
		}else if($(".autho_ta_1:eq("+y+")").find("p").attr("class")=="atl at_h"){
			st="false";
		}
		$.ajax({
			url: "aut_emp",
			data:{
				yid:$(".autho_ta_1:eq("+y+")").attr("id"),
				sf:st,
				aut:"pes",
				empid:empid
			},
			async: false,
			success: function(daa){
				$.ajax({
					url: "aut_emf",
					type:"post",
					data:{
						empid:empid,
						funid:funid,
						fun:fun
					},
					traditional: true,
					success: function(data){
						
					}
				});
			}
		});
	}
	alert("保存成功！");
}